var toastTrigger = document.getElementById("liveToastBtn");
var toastLiveExample = document.getElementById("liveToast");
var toast = new bootstrap.Toast(toastLiveExample);
var me_uto = document.querySelector(".me-auto");
var toast_body = document.querySelector(".toast-body");
var btn_signup = document.querySelector(".btn-signup");
btn_signup.addEventListener("click", function () {
  var username = document.getElementById("signup-username").value;
  var password = document.getElementById("signup-password").value;
  var urlencoded = new URLSearchParams();
  urlencoded.append("username", username);
  urlencoded.append("password", password);

  var requestOptions = {
    method: "POST",
    body: urlencoded,
    redirect: "follow",
  };

  fetch(apirUrl + "/api/reguser", requestOptions)
    .then((response) => response.json())
    .then((result) => {
      console.log(result);
      if (result.status === 0) {
        me_uto.innerHTML = "注册成功";
        toast_body.innerHTML = "注册成功,可点击登录按钮进行登录！";
        toast.show();
      }
      if (result.status === 1) {
        if (contains(result.message, "username")) {
          me_uto.innerHTML = "注册失败";
          toast_body.innerHTML = "用户名应该为字母数字组合, 且不超过10个字符";
          toast.show();
        } else if (contains(result.message, "password")) {
          me_uto.innerHTML = "注册失败";
          toast_body.innerHTML =
            "密码应该为字母数字组合的 <strong>6-12</strong> 位字符";
          toast.show();
        } else if (contains(result.message, "用户名已被占用")) {
          me_uto.innerHTML = "注册失败";
          toast_body.innerHTML = result.message;

          toast.show();
        }
      }
    })
    .catch((error) => console.log("error", error));
});
